<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%order}}".
 *
 * @property integer $id
 * @property string $order_no
 * @property integer $user_id
 * @property string $total
 * @property string $quantity
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $order_status
 * @property integer $payment
 * @property string $comment
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName(){
        return '{{%order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['user_id', 'created_at', 'updated_at', 'order_status', 'payment','quantity'], 'integer'],
            [['total'], 'number'],
            [['order_no'], 'string', 'max' => 16],
            [['comment'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'id' => '订单id',
            'order_no' => '订单序号',
            'user_id' => '用户ID',
            'total' => '订单总金额',
            'quantity' => '总件数',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'order_status' => '订单状态',
            'payment' => '支付方式',
            'comment' => '订单备注',
        ];
    }

    public static function add($cart){
        $attribute['total'] = 0;
        $attribute['quantity'] = 0;
        $attribute['order_no'] = date('ymdHms').str_pad(mt_rand(1,9999),4,'0',STR_PAD_LEFT);
        $attribute['user_id'] = Yii::$app->user->identity->getId();
        $attribute['created_at'] = time();
        $attribute['updated_at'] = time();
        $attribute['order_status'] = 1;
        $attribute['payment'] = 1;
        $attribute['comment'] = '';
        $orderProducts = [];
        foreach ($cart as $cartProduct)
        {
            $product = Product::findOne($cartProduct['id']);
            $attribute['total'] += $product['product_price']*$cartProduct['quantity'];
            $attribute['quantity'] += $cartProduct['quantity'];
            //生成订单商品数组
            $orderProduct = [
                'product_id'=>$product->id,
                'product_name'=>$product->product_name,
                'product_price'=>$product->product_price,
                'quantity'=>$cartProduct['quantity'],
                'total'=>$product['product_price']*$cartProduct['quantity'],
            ];
            $orderProducts[] = $orderProduct;
        }
        $order = new Order();
        $order->attributes = $attribute;
        $order->save();

        //存储订单商品
        foreach ($orderProducts as $orderProduct)
        {
            $orderProduct['order_id'] = $order->id;
            $op = new OrderProduct();
            $op->attributes = $orderProduct;
            $op->save();
        }

        $res = Order::find()->asArray()->with('orderProducts')->where(['{{%order}}.id'=>$order->id])->one();
        return $res;
    }

    public function getOrderProducts()
    {
        return $this->hasMany(OrderProduct::className(),['order_id'=>'id']);
    }
}
